TailsConfig.registerFormat({
  name: 'xfolk',
  topNodeClass: 'xfolkentry',

  simpleValueClasses: ["description"],

  customBindings: {
    taggedlink: function(node, object) {
      object.taggedlink = {};
      if (node.nodeName.toLowerCase() == 'a'
          && node.getAttribute('title'))
        object.taggedlink.name = node.getAttribute('title');
      else
        object.taggedlink.name = FormatBase.toText(node);
      object.taggedlink.url =  FormatBase.toURL(node);
      return true;
    }
  },

  _checkNode: function(node, object) {
    if (node.nodeName.toLowerCase() == 'a'
        && node.getAttribute("rel")
        && node.getAttribute("rel") == "tag") {

      var tag = {};
      tag.name = FormatBase.toText(node);
      tag.url = FormatBase.toURL(node);

      if (!object.tags) object.tags = [];
      object.tags.push(tag);
    }
  }, 

  _getLabel: function(object) {
    return object.taggedlink ? object.taggedlink.name : null;
  },
  _getURL: function(object) {
    return object.taggedlink ? object.taggedlink.url : null;
  },
  _getDescription: function(object) {
    var tagString = "";
    if (object.tags) {
      for (var i=0; i<object.tags.length; i++) {
        if (i != 0) tagString += ", ";
        tagString += object.tags[i].name;
      }
    }
    if (tagString == "") tagString = TailsNS.getMessage("xfolk_no_tags");
    else tagString = TailsNS.getMessage("xfolk_desc_label", [tagString]);
    return tagString;
  },
  end: null
});
